<?php
//01. 推送下载地址到队列
/**
 * ring_news_format.state
 * 0 远程下载地址未推送到队列
 * 1 已推送到下载地址，未下载到本地，【等待下载到本地】
 * 2 已下载到本地，并已更新本地下载地址，【等待上传到云】
 * 3 已上传到云端，并更新云下载地址
 * 20 上传到云端中，【等待上传完毕】
 * 
 */
//error_reporting(E_ALL);
//ini_set('display_errors', TRUE);
//ini_set('display_startup_errors', TRUE);

set_time_limit(0);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/Image.class.php');
include_once('../lib/Http.class.php');
include_once('../lib/Cut.class.php');
include_once('../lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

//$model = new Model('http://www.lnpan.com/index.php?m=api');
$model = new Db;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||lnpan_queue');

$limit = 20;
while($limit--) {
  $stop = $redis->get('LNPAN_QUEUE_STOP');
  if($stop) {
    sleep(3);
    break;
  }

  $list = $model->query('select id,news_id,format_url from ring_news_format where state=0 order by id asc limit 100');

  if($list && count($list) > 0) { 
    $ids = array();
    foreach ($list as $item) {
      $redis->rPush(KEY_QUEUE_TO_DOWN, $item['format_url']);
      $ids[]  = $item['id'];
    }

    $ids = implode(',', $ids);
    $model->query('update ring_news_format set state=1 where id in('.$ids.')');

    echo2("$ids ring_news_format set state=1");

  } else {
    echo2("ring_news_format state=1 empty");
    sleep(5);
    continue;
  }

}

$model->close();
$redis->hDel(MH_PIDS, $pid);
